import advView from "$v/advView.ejs"
import {
    isLoginRequest,
    outLoginRequest
} from "../api/admin"
export default (async (req, res, next) => {
    try {
        await isLoginRequest()
    } catch (e) {
        alert("有异常" + e.message)

        // 判断是否权限异常
        if (!e.isLogin) {
            return router.go("/login")
        }
    }
    // console.log(req.url);
    next(advView({
        subRoute: res.subRoute(),
        url: req.url
    }));

    // 获取退出登录按钮
    const oOutLogin = document.getElementById("outLogin")
    // console.log(oOutLogin);
    oOutLogin.onclick = async function () {
        try {
            await outLoginRequest()
            // 退出登录，清楚token
            localStorage.removeItem("user_token")
            // 跳转到登陆页面
            router.go("/login")
        } catch (e) {
            alert("退出登录异常" + e.message)
        }
    }
})